package com.aspire.boc.mms.wmf.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.alibaba.fastjson.JSON;
import com.aspire.boc.mms.wmf.model.SecurityData;
import com.aspire.boc.mms.wmf.model.SecurityGroup;
import com.aspire.boc.mms.wmf.model.SecurityGroupUserRelation;
import com.aspire.boc.mms.wmf.model.SecurityUser;

import net.sf.json.JSONException;

/**
 * 本地权限文件加载器
 * @author zhanghao
 *
 */
public class LocalSecurityLoader {

	private static final Log logger = LogFactory.getLog(LocalSecurityLoader.class);
	
	private static final SecurityData securityData;
	
	/**
	 *  本地权限文件名
	 */
	private static final String SECURITY_FILENAME = "security.json";
	
	static {
		
		securityData = init();
	}
	
	/**
	 * 读取配置文件，转化为json对象，初始化正则表达式
	 */
	private static SecurityData init() {
		
		URL resource = LocalSecurityLoader.class.getClassLoader().getResource(SECURITY_FILENAME);
		if(resource == null) {
			throw new RuntimeException("fail to init LocalSecurityLoader, local security config file["+SECURITY_FILENAME+"] not exists!");
		}
		String cfgFilePath = resource.getPath();
		try {
			logger.info("begin to read local security config file in path: " + cfgFilePath);
			String cfgStr = FileUtils.readFileToString(new File(cfgFilePath));
			logger.info("begin to convert local security config string to json object, cfgtStr: ["+cfgStr+"]");
			SecurityData  securityData = JSON.parseObject(cfgStr, SecurityData.class);
			if(securityData != null) {
				// 用户组map
				Map<Integer, SecurityGroup> groupMap = new HashMap<Integer, SecurityGroup>();
				List<SecurityGroup> securityGroups = securityData.getGroups();
				if(securityGroups != null) {
					for(SecurityGroup securityGroup : securityGroups) {
						groupMap.put(securityGroup.getGid(), securityGroup);
					}
					securityData.setGroupMap(groupMap);
				}
				// 用户map
				List<SecurityUser> securityUsers = securityData.getUsers();
				// 用户/用户组关联map
				Map<String, List<SecurityGroup>> userGroupMap = new HashMap<String, List<SecurityGroup>>();
				List<SecurityGroupUserRelation> groupUserRelations = securityData.getGroupUserRelations();
				if(securityUsers != null) {
					Map<String, SecurityUser> userMap = new HashMap<String, SecurityUser>();
					for(SecurityUser securityUser : securityUsers) {
						userMap.put(securityUser.getLoginName(), securityUser);
						if(securityUsers != null) {
							List<SecurityGroup> groupList = new ArrayList<SecurityGroup>();
							for(SecurityGroupUserRelation rela : groupUserRelations) {
								if(rela.getUid() == securityUser.getUid()) {
									SecurityGroup securityGroup = groupMap.get(rela.getGid());
									if(securityGroup != null) {
										groupList.add(securityGroup);
									}
								}
							}
							userGroupMap.put(securityUser.getLoginName(), groupList);
						}
					}
					securityData.setUserMap(userMap);
					securityData.setUserGroupMap(userGroupMap);
				}
			}
			logger.info("success to convert local security config string to json object: "+cfgStr);
			return securityData;
		} catch (IOException e) {
			logger.error("fail to load local security file in path : " + cfgFilePath, e);
		} catch (JSONException e) {
			logger.error("fail to convert  local security file to json in path : " + cfgFilePath, e);
		}
		return null;
	}

	public static SecurityData getSecuritydata() {
		return securityData;
	}

}
